package org.example.chinesedays.controller;


import cn.hutool.core.bean.BeanUtil;
import lombok.RequiredArgsConstructor;
import org.example.chinesedays.entity.po.ChineseDay;
import org.example.chinesedays.entity.vo.chineseDaysVO;
import org.example.chinesedays.service.IChineseDayService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.YearMonth;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Jisoo
 * @since 2025-04-22
 */
@RestController
@RequestMapping("/holidays")
@RequiredArgsConstructor
public class ChineseDayController {
    private final IChineseDayService chineseDayService;

    @GetMapping("/{year}")
    public List<chineseDaysVO> getHolidaysByYear(@PathVariable String year) {
        List<ChineseDay> list = chineseDayService.lambdaQuery().eq(ChineseDay::getYear, year).list();
        return list.stream().map(c-> BeanUtil.copyProperties(c,chineseDaysVO.class)).collect(Collectors.toList());
    }

    @GetMapping("/{year}/{month}")
    public List<chineseDaysVO> getHolidaysByMonth(@PathVariable String year, @PathVariable String month) {
        LocalDate start=LocalDate.of(Integer.parseInt(year),Integer.parseInt(month),1);
        LocalDate end= YearMonth.of(Integer.parseInt(year), Integer.parseInt(month))
                .atEndOfMonth();
        List<ChineseDay> list = chineseDayService.lambdaQuery().eq(ChineseDay::getYear, year).between(ChineseDay::getDate, start, end).list();
        return BeanUtil.copyToList(list,chineseDaysVO.class);
    }
    @GetMapping("/{year}/{month}/{day}")
    public chineseDaysVO getHolidaysByDay(@PathVariable String year, @PathVariable String month, @PathVariable String day) {
        ChineseDay one = chineseDayService.lambdaQuery().eq(ChineseDay::getYear, year)
                .eq(ChineseDay::getDate, LocalDate.of(Integer.parseInt(year), Integer.parseInt(month), Integer.parseInt(day))).one();
        return BeanUtil.copyProperties(one,chineseDaysVO.class);
    }
}
